import React, { Component } from 'react'
import './index.css';

export default class Main extends Component {
    render() {
        let {todos} = this.props;
        return (
            <ul className="todo-main">
                {
                    todos.map(item => {
                        return <li key={item.id}>
                                    <label>
                                        <input checked={item.done} onChange={this.checkTodo(item.id)} type="checkbox" />
                                        <span>{item.title}</span>
                                    </label>
                                    <button
                                        className="btn btn-danger"
                                        style={{display: item.done ? "block" : "none"}}
                                        tid={item.id}
                                        onClick={this.delTodo}
                                    >
                                        删除
                                    </button>
                                </li>
                    })
                }
                {
                    todos.length <= 0 && <li className="empty">暂无数据</li>
                }
                
            </ul>
        )
    }

    //修改任务的状态
    checkTodo = (id) => {
        return e => {
            // console.log(e.target.checked);
            this.props.checkTodo(id, e.target.checked);
        }
    }

    //删除某个任务
    delTodo = (e) => {
        //获取要删除的 id
        let id = (e.target.getAttribute('tid')); //NaN
        this.props.removeTodo(id);
    }
}
